package com.wenlong.controller;

import com.google.gson.Gson;
import com.wenlong.pojo.User;
import com.wenlong.service.UserService;
import com.wenlong.service.impl.UserServiceImpl;
import com.wenlong.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author 苏文龙
 * @version 1.0
 */
@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"})
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
    }
    protected void existsUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean exists = userService.exitsUser(username);
        Map<String, Object> map = new HashMap<>();
        Gson gson = new Gson();
        map.put("exists", exists);
        String data = gson.toJson(map);
        resp.getWriter().write(data);
    }
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = WebUtils.injectParameterToBean(req.getParameterMap(),new User());
        User user1 = userService.login(user);
        if (user1 == null) {
            req.setAttribute("msg","用户名或密码错误");
            req.setAttribute("username",username);
            req.getRequestDispatcher("/pages/user/Login.jsp").forward(req,resp);
        }else {
            req.getSession().setAttribute("user",user1);
            req.getRequestDispatcher("/pages/user/LoginSuccess.jsp").forward(req,resp);
        }
    }

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String codeTaken = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        if (codeTaken != null && codeTaken.equals(code)) {
            if (userService.exitsUser(username)) {
                req.setAttribute("username",username);
                req.setAttribute("email",email);
                req.setAttribute("msg","用户名已存在");
                req.getRequestDispatcher("/pages/user/Regist.jsp").forward(req, resp);
            } else {
                User user = WebUtils.injectParameterToBean(req.getParameterMap(),new User());
                userService.registUser(user);
                req.getRequestDispatcher("pages/user/RegistSuccess.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            req.setAttribute("msg","验证码错误");
            req.getRequestDispatcher("/pages/user/Regist.jsp").forward(req, resp);
        }
    }
}
